# -*- encoding: utf-8 -*-
import json
import sys
sys.path.append('../public') 
import os
import requests

def get_url(url, params=None, proxies=None):
    rsp = requests.get(url, params=params, proxies=proxies)
    rsp.raise_for_status()
    # rsp.encoding = 'utf-8'
    return rsp.text
    
def getData(scale,datalen):
	url = "https://quotes.sina.cn/cn/api/json_v2.php/CN_MarketDataService.getKLineData?"
	url = url + "symbol=sh000001&scale="+ str(scale) +"&ma=&datalen=" + str(datalen)
	return get_url(url)

def 保存文本(filename,contents):
	fh = open(filename,'w')
	fh.write(contents)
	fh.close()

def 获取文本(txtPath,encoding='utf-8'):
	with open(txtPath,'r',encoding=encoding) as f:
		content = f.read()
	return content

def 读取json文件(jsonPath,encoding='utf-8'):
	with open(jsonPath,'r',encoding=encoding) as f:
		j = json.load(f)
	return j

def 读取json字符串(jsonStr):
	j = json.loads(jsonStr)
	return j	

def isUp(ma,lastma):
	val = False
	if(ma>lastma):
		val = True
	return val

def getJsonData(jsonPath):
	if os.path.exists(jsonPath):
		print("读取本地文件！")
		val = 读取json文件(jsonPath)
	else:
		print("文件不存在！准备下载！")
		da = getData(15,900)
		保存文本(jsonPath,da)
		val = 读取json文件(jsonPath)
	return val

def UpDownVal(ma5,lastma5,ma10,lastma10,ma30,lastma30):
	val = False
	if(isUp(ma5,lastma5) and isUp(ma10,lastma10) and isUp(ma30,lastma30)):
		if(isUp(ma5,ma10) and isUp(ma10,ma30)):
			val = True
	return val

jsonPath = os.getcwd() + "/sina15.json"
da = getJsonData(jsonPath)

print(type(da))
print(len(da))
# print(da[20]['ma_price30'])

dest=[]
mon=[]
# for i in range(1,len(da)):
for i in range(1,len(da)):
	ma5 = da[i]['ma_price5']
	lastma5 = da[i-1]['ma_price5']
	ma10 = da[i]['ma_price10']
	lastma10 = da[i-1]['ma_price10']
	ma30 = da[i]['ma_price30']
	lastma30 = da[i-1]['ma_price30']
	maval = UpDownVal(ma5,lastma5,ma10,lastma10,ma30,lastma30)

	vol5 = da[i]['ma_volume5']
	lastvol5 = da[i-1]['ma_volume5']
	vol10 = da[i]['ma_volume10']
	lastvol10 = da[i-1]['ma_volume10']
	vol30 = da[i]['ma_volume30']
	lastvol30 = da[i-1]['ma_volume30']
	volval = UpDownVal(vol5,lastvol5,vol10,lastvol10,vol30,lastvol30)

	# print(maval)
	# print(volval)

	if(maval and volval):
		print('BUY'+da[i]['day'])
		bp=float(da[i]['high'])
		for j in range(i,len(da)):
		    maj5 = da[j]['ma_price5']
		    maj30 = da[j]['ma_price30']
		    if(maj5<maj30):
		        print('SEL'+da[j]['day'])
		        
		        sp=float(da[j]['low'])
		        #print(bp)
		        #print(sp)
		        print(sp-bp)
		        d=[da[i]['day'],da[j]['day'],sp-bp]
		        dest.append(d)
		        mon.append([sp-bp])
		        break
		    

print(max(mon))
print(min(mon))
#print(sum(float(mon)))
result=0
for i in mon:
    result=result+float(i[0])
    
print('结果'+ str(result))    
	# print(i['day'])
	# mainfo = UpDownVal(i['ma_price5'],lastma5,ma10,lastma10,ma30,lastma30)
	# print(i['ma_price30'])
# ma5 = da[1]['ma_price5']
# lastma5 = da[0]['ma_price5']
# # print(da[])
# print(da.count('ma_price5'))
# print(ma5)
# print(lastma5)






# print(vol5)
# print(lastvol5)
# print(vol10)
# print(lastvol10)
# print(vol30)
# print(lastvol30)

# print(UpDownVal(ma5,lastma5,ma10,lastma10,ma30,lastma30))

# print(UpDownVal(vol5,lastvol5,vol10,lastvol10,vol30,lastvol30))

